package vsvm.gui.controls;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JToggleButton;

import vsvm.gui.Environment;
import vsvm.gui.VSVMInternalFrame;

public class TaskBarButton extends JToggleButton {
	private static final long serialVersionUID = 100L;
		
			
		public static final int BAR_BUTTON_HEIGHT = 20;
		public static final int BAR_BUTTON_WIDTH  = 70;		
		
		private TaskBar taskBar = null;
		public  VSVMInternalFrame iFrame = null;
		
		public TaskBarButton(VSVMInternalFrame iFrame, TaskBar taskBar) {
			this.iFrame  = iFrame;
			iFrame.setTaskBarButton(this);
			
			this.taskBar = taskBar;
			this.setPreferredSize(new Dimension(BAR_BUTTON_WIDTH, BAR_BUTTON_HEIGHT));
			this.setText(iFrame.getTitle());
			this.setToolTipText(iFrame.getTitle());
			
			this.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					
					
					
					
						
				
					
					if (TaskBarButton.this.iFrame.getState() == VSVMInternalFrame.STATE_MINIMIZED)
					try {
						TaskBarButton.this.iFrame.setIcon(false);
						TaskBarButton.this.iFrame.setVisible(true);						
					} catch (Exception ex) {
						
					} else {
						if (!TaskBarButton.this.isSelected()) {
							try {
								TaskBarButton.this.iFrame.setIcon(true);
								TaskBarButton.this.iFrame.setVisible(false);								
							} catch (Exception ex) {
								
							}
						} else 							
							TaskBarButton.this.iFrame.moveToFront();
					}			
				}
			});			
			
			this.addItemListener(new ItemListener(){
				public void itemStateChanged(ItemEvent e) {
					if (e.getStateChange() == ItemEvent.SELECTED) {
						TaskBarButton last = TaskBarButton.this.taskBar.getLastSelected();
						if (last != null) last.setSelected(false);
						TaskBarButton.this.taskBar.setLastSelected(TaskBarButton.this);
					}
					
				}
			});
			
		}		
}

